#!/usr/bin/env python
#
#       MenuEntity.py
#
#       Copyright 2008 karthikeyan S.<kaarthikeyapreyan@gmail.com>
#
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import os
import wx
import glob

import resultdefinitions

class Viewproperty:

    def __init__(self,baseframe):
        """
        The class is invalid without the result file.
        """
        self.Inst=baseframe

    def viewgenerated(self,event):
        """
        Display generated file
        """
        generated_files=glob.glob('pytestgen_*.py')
        self.subframe=wx.Frame(self.Inst,-1,'Check')
        wx.StaticText(self.subframe,-1,"List of Generated files",style=wx.OK|wx.MAXIMIZE|wx.MINIMIZE)
        sub=50 #To provide space between the static text and button
        for entries in generated_files:
            wx.Button(self.subframe,-1, "%s"%entries,(50,sub))
            sub=sub+50
        wx.Button(self.subframe,1,"Close",(200,sub))
        self.subframe.Fit()
        self.subframe.Show()

        self.subframe.Bind(wx.EVT_BUTTON,self.displayselected)

    def viewruntest(self,event):
        """
        Display the results of the generated file
        """
        if os.path.isfile("PyTestResults.txt"):
            result=open("PyTestResults.txt",'r')
            self.stats=result.readlines()
            self.module=self.stats.\
                pop(0).split(":")[1].rstrip('\n').lstrip(" ")
        else:
            self.stats=[]
            self.module=None
        resultdefinitions.StatDisp(self.Inst,-1,"PyTest Results",\
            ["Results Summary for Module : %s"%self.module,self.stats],flag=1)

    def displayselected(self,event):

        if event.GetEventObject().GetId() == 1:
            self.subframe.Close()
        else:
            selected=event.GetEventObject().GetLabel()
            fileobj=open(selected)
            data= fileobj.readlines()
            resultdefinitions.StatDisp(self.Inst,-1,\
                "PyTest Generated",['Generated TestFile',data],flag=1)
